package com.mao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mao.common.Result;
import com.mao.entity.Category;
import com.mao.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理(管理菜品总类和套餐总类)
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService cs;


    /**
     * 新增菜品或套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Category category){
        if (!cs.save(category)) {
            return Result.error("新增失败");
        }
        return Result.success("新增成功");
    }

    /**
     * 分页查询分类数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize){
        Page pageInfo = cs.get_page(page, pageSize);
        return Result.success(pageInfo);
    }


    /**
     * 删除分类数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long ids){
        cs.remove(ids);
        return Result.success("删除成功");
    }

    /**
     * 更新分类数据
     * @param category
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Category category){
        cs.updateById(category);
        return Result.success("更新成功");
    }

    /**
     * 获取所有分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> list( Category category){
        List<Category> categoryList = cs.get_list(category);
        return Result.success(categoryList);
    }

}
